package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.ContainerType;
import com.meilingcloud.biobank.service.IContainerTypeService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.constant.UserConstants;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 容器类型Controller
 *
 * @author chenl
 * @date 2021-03-03
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/containerType")
public class ContainerTypeController extends BaseController {

    private final IContainerTypeService iContainerTypeService;

    /**
     * 查询容器类型列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:containerType:list')")
    @GetMapping("/list")
    public AjaxResult list(ContainerType containerType) {
        List<ContainerType> list = iContainerTypeService.queryList(containerType);
        return AjaxResult.success(list);
    }

    /**
     * 根据编码查询子容器列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:containerType:list')")
    @GetMapping("/listChildren/{containerTypeCode}")
    public AjaxResult listChildren(@PathVariable("containerTypeCode") String containerTypeCode) {
        List<ContainerType> list = iContainerTypeService.queryChildren(containerTypeCode);
        return AjaxResult.success(list);
    }

    /**
     * 获取容器类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:containerType:query')")
    @GetMapping(value = "/{containerTypeId}")
    public AjaxResult getInfo(@PathVariable("containerTypeId") Long containerTypeId) {
        return AjaxResult.success(iContainerTypeService.queryById(containerTypeId));
    }

    /**
     * 新增容器类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:containerType:add')")
    @Log(title = "容器类型 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody ContainerType containerType) {
        if (UserConstants.NOT_UNIQUE.equals(iContainerTypeService.checkCodeUnique(containerType))) {
            return AjaxResult.error("新增容器类型" + containerType.getContainerTypeName() + "-"
                    + containerType.getContainerTypeCode() + "失败，编号已存在");
        }
        containerType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iContainerTypeService.save(containerType));
    }

    /**
     * 修改容器类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:containerType:edit')")
    @Log(title = "容器类型 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody ContainerType containerType) {
        containerType.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iContainerTypeService.updateContainerById(containerType));
    }

    /**
     * 删除容器类型
     */
    @PreAuthorize("@ss.hasPermi('biobank:containerType:remove')")
    @Log(title = "容器类型 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{containerTypeIds}")
    public AjaxResult remove(@PathVariable Long[] containerTypeIds) {
        return toAjax(iContainerTypeService.removeContainerByIds(Arrays.asList(containerTypeIds)));
    }
}
